﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Eloquera.Client;

namespace _430GroupProject
{
    //A single unit of work in a timesheet.  A number of hours on a day.
    [DataContractAttribute]
    [KnownType(typeof(DateTime))]
    public class WorkSession : SuperClass
    {
        /// <summary>
        /// Holds the Unique ID
        /// </summary>
        [DataMember, Ignore]
        public long DBID { get; set; } 

        /// <summary>
        /// The total number of hours work during this work session.
        /// </summary>
        [DataMember]
        public double TotalHours { get; set; }

        /// <summary>
        /// The user who worked the hours.
        /// </summary>
        [DataMember]
        public String UserID { get; private set; }

        /// <summary>
        /// The day the hours were worked.  This is only used for the Month and Day properties. Hours, mins, second and other smaller precision values are ignored.
        /// </summary>
        [DataMember]
        public DateTime Date { get; set; }

        [DataMember]
        public DateTime PayStart { get; set; }

        [DataMember]
        public DateTime PayEnd { get; set; }

        /// <summary>
        /// Creates a new WorkSesion with the hours set to negative one (-1). 
        /// </summary>
        /// <param name="userID">The user who worked the hours.</param>
        /// <param name="Time">The Day of this work session</param>
        public WorkSession(String userID, DateTime Time) : this(userID, Time, -1, Time, Time) { }

        /// <summary>
        /// Creates a new WorkSession with the given number of hours.
        /// </summary>
        /// <param name="userID">The user who worked the hours.</param>
        /// <param name="Time">The Day of this work session.</param>
        /// <param name="hours">The number of hours the user worked on the given day.</param>
        public WorkSession(String userID, DateTime Time, double hours, DateTime payS, DateTime payE)
        {
            this.UserID = userID;
            this.TotalHours = hours;
            this.Date = Time;
            this.PayStart = payS;
            this.PayEnd = payE;
            this.DBID = -1;
        }
    }

    //One type of work session that involves the user clocking in and then clocking out.
    [DataContractAttribute]
    [KnownType(typeof(DateTime))]
    public class ClockInSession : WorkSession
    {
        /// <summary>
        /// Unique Identifier for worksessions, used by DB.
        /// </summary>
        [DataMember, ID]
        public long DBID;

        /// <summary>
        /// True if the user is Clocked In, but hasn't Clocked Out yet.
        /// </summary>
        [DataMember]
        public bool IsOpen { get; private set; }

        /// <summary>
        /// True if the user has clocked in and then clocked out.
        /// </summary>
        [DataMember]
        public bool IsClosed { get; set; }

        /// <summary>
        /// The starting time of the clock in session.
        /// </summary>
        [DataMember]
        public DateTime StartTime { get; private set; }

        /// <summary>
        /// The ending time of the clock in session.
        /// </summary>
        [DataMember]
        public DateTime EndTime { get; private set; }

        public ClockInSession(String UserID)
            : base(UserID, DateTime.Now)
        {
            IsOpen = false;
            IsClosed = false;
        }

        public bool ClockIn()
        {
            if (IsOpen || IsClosed) return false;
            IsOpen = true;
            StartTime = DateTime.Now;
            Date = StartTime;
            return true;
        }

        public bool ClockOut()
        {
            if (!IsOpen || IsClosed)
            {
                return false;
            }
            IsOpen = false;
            IsClosed = true;
            EndTime = DateTime.Now;
            TimeSpan difference = EndTime.Subtract(StartTime);
            TotalHours = difference.TotalHours;
            return true;
        }
    }
}
